package com.galaxy.coupon.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.galaxy.coupon.domain.PointsExchangeRule;
import com.galaxy.coupon.domain.bo.PointsExchangeRuleBo;
import com.galaxy.coupon.domain.convert.PointsExchangeRuleConvert;
import com.galaxy.coupon.domain.vo.PointsExchangeRuleVo;
import com.galaxy.coupon.mapper.CouponTemplateMapper;
import com.galaxy.coupon.mapper.PointsExchangeRuleMapper;
import com.galaxy.coupon.service.ICouponTemplateService;
import com.galaxy.coupon.service.IPointsExchangeRuleService;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 积分兑换规则Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class PointsExchangeRuleServiceImpl implements IPointsExchangeRuleService {

    private final PointsExchangeRuleMapper baseMapper;
    private final CouponTemplateMapper couponMapper;

    @DS("slave")
    @Override
    public TableDataInfo<PointsExchangeRuleVo> queryPage(PointsExchangeRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PointsExchangeRule> lqw = buildQueryWrapper(bo);
        Page<PointsExchangeRuleVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        page.getRecords().forEach(r -> r.setCouponName(couponMapper.selectById(r.getCouponId()).getName()));
        return TableDataInfo.build(page);
    }

    @DS("slave")
    @Override
    public PointsExchangeRuleVo getInfo(Long id) {
        PointsExchangeRuleVo pointsExchangeRuleVo = baseMapper.selectVoById(id);
        if (pointsExchangeRuleVo != null) {
            pointsExchangeRuleVo.setCouponName(couponMapper.selectById(pointsExchangeRuleVo.getCouponId()).getName());
        }
        return pointsExchangeRuleVo;
    }

    @DS("slave")
    @Override
    public TableDataInfo<PointsExchangeRuleVo> selectAllPointsExchangeRulesWithCoupons() {
        List<PointsExchangeRuleVo> page = baseMapper.selectAllPointsExchangeRulesWithCoupons();;
        return TableDataInfo.build(page);
    }

    @DS("slave")
    @Override
    public PointsExchangeRuleVo selectPointsExchangeRuleWithCouponById(Long id) {
        return baseMapper.selectPointsExchangeRuleWithCouponById(id);
    }

    @Override
    public Boolean insertByBo(PointsExchangeRuleBo bo) {
        PointsExchangeRule add = PointsExchangeRuleConvert.INSTANCE.convert(bo);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(PointsExchangeRuleBo bo) {
        PointsExchangeRule update = PointsExchangeRuleConvert.INSTANCE.convert(bo);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PointsExchangeRule entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<PointsExchangeRule> buildQueryWrapper(PointsExchangeRuleBo bo) {
        LambdaQueryWrapper<PointsExchangeRule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getId() != null, PointsExchangeRule::getId, bo.getId());
        lqw.eq(bo.getCouponId() != null, PointsExchangeRule::getCouponId, bo.getCouponId());
        lqw.eq(bo.getPoints() != null, PointsExchangeRule::getPoints, bo.getPoints());
        lqw.eq(bo.getStatus() != null, PointsExchangeRule::getStatus, bo.getStatus());
        return lqw;
    }
}
